package com.wonderful.eyepetizer.utils

import android.os.Build
import android.os.Build.VERSION.SDK_INT
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.ImageLoader
import coil.compose.rememberAsyncImagePainter
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/18 10:21
 */

const val IMAGE_PNG_URL =
    "https://upload.wikimedia.org/wikipedia/commons/4/4c/Android_Marshmallow.png"
const val IMAGE_SVG_URL =
    "https://upload.wikimedia.org/wikipedia/commons/e/e0/Android_robot_%282014-2019%29.svg"
const val GIF_URL =
    "https://upload.wikimedia.org/wikipedia/commons/c/cb/Android_easter_eggs_%28version_2.3_to_10%29.gif"

/**
 * Composable function to display an image using Image component
 */
@Composable
fun ImageComponent(imgUrl: String, imgLoader: ImageLoader, contentDesc: String) {
    Image(
        painter = rememberAsyncImagePainter(model = imgUrl, imageLoader = imgLoader),
        contentDescription = contentDesc,
        modifier = Modifier
            .padding(16.dp)
            .size(200.dp)
    )
}

@Preview(showBackground = true)
@Composable
fun CoilPNGTest() {
    val imgLoader = ImageLoader.Builder(LocalContext.current).build()
    ImageComponent(imgUrl = IMAGE_PNG_URL, imgLoader = imgLoader, contentDesc = "png test")
}

@Preview(showBackground = true)
@Composable
fun CoilSVGTest() {
    val imgLoader = ImageLoader.Builder(LocalContext.current)
        .components {
            add(SvgDecoder.Factory())
        }.build()
    ImageComponent(imgUrl = IMAGE_SVG_URL, imgLoader = imgLoader, contentDesc = "svg test")
}

@Preview(showBackground = true)
@Composable
fun CoilGIFTest() {
    val imgLoader = ImageLoader.Builder(LocalContext.current)
        .components {
            if (SDK_INT >= Build.VERSION_CODES.P) {
                add(ImageDecoderDecoder.Factory())
            } else {
                add(GifDecoder.Factory())
            }
        }.build()
    ImageComponent(imgUrl = GIF_URL, imgLoader = imgLoader, contentDesc = "gif test")
}